package com.example.reviewdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.reviewdemo.adapter.ArticleAdapter;
import com.example.reviewdemo.bean.Article;
import com.example.reviewdemo.utils.DateUtil;
import com.example.reviewdemo.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    private boolean articleFetched = false;

    private RecyclerView recyclerView;


    private ArticleAdapter articleAdapter;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rlv_article);

        getArticle();

    }

    private void getArticle() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtil.getResult();
                Message message = new Message();
                message.what = 1;
                message.obj = result;
                handler.sendMessage(message);
            }
        }).start();
    }


    private Handler handler = new Handler(Objects.requireNonNull(Looper.myLooper())) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String json = msg.obj.toString();

                Gson gson = new Gson();
                JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
                JsonObject dataObject = jsonObject.getAsJsonObject("data");
                JsonArray datasArray = dataObject.getAsJsonArray("datas");


                List<Article> articleList = new ArrayList<>();
                // 遍历 datas 数组中的元素
                for (JsonElement element : datasArray) {
                    JsonObject itemObject = element.getAsJsonObject();
                    Article article = new Article();
                    article.setTitle(String.valueOf(itemObject.get("title").getAsString()));
                    article.setAuthor(String.valueOf(itemObject.get("author").getAsString()));
                    article.setLink(String.valueOf(itemObject.get("link").getAsString()));
                    articleList.add(article);
                }

                articleAdapter = new ArticleAdapter(MainActivity.this, articleList);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
                recyclerView.setAdapter(articleAdapter);
                recyclerView.setLayoutManager(linearLayoutManager);
            }
        }
    };

}